# TODO: Still need to find a good way to read in the stupid locations this year .txt file. 
# the problem is that lines have uneaven number of data. TERRIBLE. Want this to be robust. 

# TODO: make sure that all stations spatial dataframes are being saved and start potting 
# TODO: bitches! 

# create statial points: http://www.maths.lancs.ac.uk/~rowlings/Teaching/UseR2012/introductionTalk.html

# the purpose of this script is to load excel data into R so that I never have to look at an excel 
# spread sheet again. 

# NOTE: excel2csv.applescript must be run before this script will work! 

library(sp)
library(chron)
dsn <- "xls/"

# names of the station specific spreadsheats of data 
csv_names <- c("FR1_mike_20130209-20130601",  "Precip N_s17_20121111-20130531",    "R2_s9_20130208-20130531",  
            "RF_s8_20121018-20130208",  "precipL_s7_20121011-20130601", "FR2_s10_20121018-20121229",  
            "PrecipAA_bunch_20121018-20130203",  "R3_s13_20121018-20130205",  "RF_s8_20130208-20130603",
            "precip_B_s1_20121017-20130601", "FR2_s10_20130209-20130601",   "Precip_K_s15x_20121017-20130601",   
            "R3_s13_20130208-20130603","FR1_mike_20121111-20130209",  "PI_s3_20121111-20130601",     
            "R2_s9_20121018-20130208",  "R5_s16_20121017-20130601" )


# if spreadsheets of data are updated change to true to load and save new data
loadCSV = TRUE
if (loadCSV) {
  # load each spreadsheet into workspace 
  for (station in csv_names[2]) {
    
    file <- paste(dsn,station,".csv",sep="")
    data <- read.csv(file=file,head=TRUE,sep=",")
    
    # add time from series begining column
    daysInMonth <-c(31,28,31,30,31,30,31,31,30,31,30,31)
    rows <- length(data[,1]) # how many rows of each stations data 
    dt <- rep(NA, rows) # initialize dt rows for time differences 
    dt[1] <- 0
    secondsPassed <- rep(0,rows) # the first index should be left as zero 
    for (i in 1:(rows-1)) {
      
      time1 <- toString(data[i+1,1]) # rows time as string
      time0 <- toString(data[i,1])  # station specific absolute time reference
      
      month1<- as.numeric(substr(time1,1,2))  # index time components and turn to ints
      day1  <- as.numeric(substr(time1,4,5))  
      hour1 <- as.numeric(substr(time1,10,11))
      min1  <- as.numeric(substr(time1,13,14))
      sec1  <- as.numeric(substr(time1,16,17))
      
      month0<- as.numeric(substr(time0,1,2))
      day0  <- as.numeric(substr(time0,4,5))
      hour0 <- as.numeric(substr(time0,10,11))
      min0  <- as.numeric(substr(time0,13,14))
      sec0  <- as.numeric(substr(time0,16,17))
      
      # a seconds counter that increases until time1 is met
      start <- c(month0,day0,hour0,min0,sec0)
      finish <- c(month1,day1,hour1,min1,sec1)
      seconds <- 0
      # NOTE: currently flying by the solution
      while (!all(start==finish)) {
        seconds <- seconds + 1 
        start[5] <- start[5] + 1   # add a second
        if (start[5]==60) {
          start[5] <- 0            # 0 seconds into new minute
          start[4] <- start[4] + 1 # advance minute
        }
        if (start[4]==60) {
          start[4] <- 0  
          start[3] <- start[3] + 1 # advance hour
        }
        if (start[3] == 24) {
          start[3] <- 0
          start[2] <- start[2] + 1 # advance day
        }
        if (start[2]==(daysInMonth[start[1]] + 1)) {
          start[2] <- 1
          start[1] <- start[1] + 1 # advance month
        }
        if (start[1]==13) {
          start[1] <- 1 # december to january
        }
        
      }
      secondsPassed[i + 1] <- seconds # from last row 
    }
    absoluteSecondsPassed <- cumsum(secondsPassed) # seconds from station start 
    
    # calculate a rain rate for each row mm/hr
    deltaRain <- rep(NA,rows)
    deltaRain[2:rows] <- diff(data[,2])
    rainRate <- deltaRain / secondsPassed # mm/s
    
    # add to the station data
    data[,5] <- secondsPassed
    data[,6] <- absoluteSecondsPassed
    data[,7] <- rainRate
    
    # save each station with a unique ID to be easily load().
    # Data name when loaded in R will be the same as the diretory name 
    save(data, file=paste("rainRData/",station,".RData",sep=""))
    
    
    # NOTE: It might be worth while creating a date and time vector seperate from eachother
    
    
  }
}


# NOTE: consider making this junk of code its own script createSpatialPoints.R or something like that 
if(FALSE) {
# set to TRUE if the locations txt file has been manaully edited to work with table.read
# NOTE: FALSE not working at this time because the format of the txt file is terrible
manualEdit <- TRUE
if (!manualEdit) {
# read ugly list of uneven lengths 
dat <- readLines("locations_this_year.txt")
station <- matrix(length(dat),4)
  for (i in length(dat)){
    line <- toString(dat[i])
    writeLines(line, "temp.txt")
    tableTemp <- read.table("temp.txt",sep=",")
    station[i] <- tableTemp[1:4]
    
    # keep only what I want from each line
    #split <- strsplit(dat[2],split=",")
    
    #dat[1] <- split[1:4]
    #read.table("locations_this_year.txt",sep=",",)
    
    
    scan("locations_this_year.txt")
  
  } 
} else { # locactions text file manually edited and can do simple read.table
  locations <- read.table("locations_this_year.txt", sep=",",
                          col.names=c("site","lat","lon","evel","sensorType"),
                          colClasses=c("character","double","double","integer","character"))
}

# combine station location info and combine with station csv data to create a spatialpointdataframe
# NOTE: currently no station2 xls data so _s2 omitted from list below 
idSignal <- c("_s1_", "_s3_",   "_s4_",   "_s6_",   "_s7_",   "_s8_",   "_s9_",  "_s10_", 
              "_s12_",  "_s13_", "_s14_",  "_s15x_", "_s16_",  "_s17_",  "_SALM_", "_BUNCH_",   "_MIKE_" )
count = 0
for (i in 1:(length(idSignal) - 1)) {
  # TODO: Fix the fact that _mike_ will be found more than once -1 on loop temp fix
  csvIndex <- grep(idSignal[i],csv_names,ignore.case=TRUE)
  if (length(csvIndex > 1)) {
    csvIndex <- csvIndex[1] # skips the second one
    count = count + 1
  } else if (length(csvIndex > 1)) {
    csvIndex <- csvIndex[2]
  }
  
  # TODO: come up with some kind of way of keeping track of which files
  # are not loaded correctly and need to be done in some other fashion
  if(! (length(csv_names[csvIndex]) < 1) ) {
    fileName <- paste("rainRData/",csv_names[csvIndex],".RData",sep="")
    load(fileName) # will load with the name "data"
    
    # pair location to the station name and create spatialdataframe
    # NOTE: There are more stations than there spreadsheets of data 
    #       "i" is the index of the location in the loop
    
    # put out coordinates
    lat <- locations[i,2]
    lon <- locations[i,3]
    coords <-cbind(lat,lon)
    # data 
    totalRain <- as.data.frame(sum(data[,2]))
    
    totalRain_spdf <- SpatialPointsDataFrame(coords,totalRain)
    
    save(totalRain_spdf,file=paste("rainRData/",locations[i,1],".RData",sep=""))
    
    
  }
}
}












